**********************************************
***** Merging WKP 2009, 2013 & 2017 **********
**********************************************


cd "SET_YOUR_PATH\GLES\"

use "wkp2017_short.dta", clear

merge m:1 lfdn_2013 using  "wkp2013_short.dta", gen(merge1317)
merge m:1 lfdn_2009 using  "wkp2009_short.dta", gen(merge091317)

gen id = _n

*Generate correct year variables
drop year
gen year = .
replace year = 2009 if year_2009==2009
replace year = 2013 if year_2013==2013
replace year = 2017 if year_2017==2017

*Recode income variables to same categories
recode income_2009 (1=1 "<500 EUR")(2=2 "500-999")(3=3 "1000-1499")(4=4 "1500-1999") ///
	(5=5 "2000-2499")(6=6 "2500-2999")(7 8=7 "3000-3999")(9 10=8 "4000-4999") ///
	(11=9 ">5000 EUR"), gen(inc_2009) 

recode income_2013 (1=1 "<500 EUR")(2 3=2 "500-999")(4 5=3 "1000-1499")(6=4 "1500-1999") ///
	(7=5 "2000-2499")(8=6 "2500-2999")(9=7 "3000-3999")(10=8 "4000-4999") ///
	(11/13=9 ">5000 EUR"), gen(inc_2013) 

recode income_2017 (1=1 "<500 EUR")(2 3=2 "500-999")(4 5=3 "1000-1499")(6=4 "1500-1999") ///
	(7=5 "2000-2499")(8=6 "2500-2999")(9=7 "3000-3999")(10=8 "4000-4999") ///
	(11/13=9 ">5000 EUR"), gen(inc_2017) 

lab var inc_2009 "Income 2009 (coherent categories)"
lab var inc_2013 "Income 2013 (coherent categories)"
lab var inc_2017 "Income 2017 (coherent categories)"

*save
order _all, alphabetic

save "wkp09-17.dta", replace


***************************
* Recode and shape long
***************************


*Indicator for panel participation
fre merge*
gen tp = .
replace tp=1 if merge1317==3 & merge091317!=3
replace tp=2 if merge1317==3 & merge091317==3
replace tp=0 if merge1317==2 & merge091317==3

lab def tp 0 "2009->2013" 1 "2013->2017" 2 "2009->2013->2017"
lab val tp tp 

drop if tp==.

*Recode variables: use only first wave of each year that includes resp. variable

clonevar polint_2009 = polint_2009_2
clonevar polint_2013 = polint_2013_1
clonevar polint_2017 = polint_2017_1

clonevar effex1_2009 = effex1_2009_1
clonevar effex1_2013 = effex1_2013_1
clonevar effex1_2017 = effex1_2017_1

clonevar effex2_2009 = effex2_2009_1
clonevar effex2_2013 = effex2_2013_1
clonevar effex2_2017 = effex2_2017_1

clonevar effint1_2009 = effint1_2009_1
clonevar effint1_2013 = effint1_2013_1
clonevar effint1_2017 = effint1_2017_1

clonevar effint2_2009 = effint2_2009_1
clonevar effint2_2013 = effint2_2013_1
clonevar effint2_2017 = effint2_2017_1

clonevar ewl_2009 = ewl_2009_2
clonevar ewl_2013 = ewl_2013_1
clonevar ewl_2017 = ewl_2017_1

clonevar imp_elec_2009 = imp_elec_2009_2
clonevar imp_elec_2013 = imp_elec_2013_2
clonevar imp_elec_2017 = imp_elec_2017_3

clonevar imp_elec_camp_2009 = imp_elec_camp_2009_2
clonevar imp_elec_camp_2013 = imp_elec_camp_2013_2
clonevar imp_elec_camp_2017 = imp_elec_camp_2017_4

clonevar inet_pol_2009 = inet_pol_2009_2
clonevar inet_pol_2013 = inet_pol_2013_2
clonevar inet_pol_2017 = inet_pol_2017_1

clonevar itv_2009 = itv_2009_1
clonevar itv_2013 = itv_2013_1
clonevar itv_2017 = itv_2017_1

clonevar partyid_2009 = partyid_2009_1
replace partyid_2009 = partyid_2009_3 if partyid_2009==.
replace partyid_2009 = partyid_2009_5 if partyid_2009==.
replace partyid_2009 = partyid_2009_7 if partyid_2009==.
clonevar partyid_2013 = partyid_2013_1
replace partyid_2013 = partyid_2013_3 if partyid_2013==.
replace partyid_2013 = partyid_2013_5 if partyid_2013==.
replace partyid_2013 = partyid_2013_7 if partyid_2013==.
clonevar partyid_2017 = partyid_2017_1
replace partyid_2017 = partyid_2017_2 if partyid_2017==.
replace partyid_2017 = partyid_2017_3 if partyid_2017==.
replace partyid_2017 = partyid_2017_4 if partyid_2017==.
replace partyid_2017 = partyid_2017_5 if partyid_2017==.
replace partyid_2017 = partyid_2017_6 if partyid_2017==.
replace partyid_2017 = partyid_2017_7 if partyid_2017==.
replace partyid_2017 = partyid_2017_8 if partyid_2017==.
replace partyid_2017 = partyid_2017_9 if partyid_2017==.

clonevar pidstr_2009 = pidstr_2009_1
replace pidstr_2009 = pidstr_2009_3 if pidstr_2009==.
replace pidstr_2009 = pidstr_2009_5 if pidstr_2009==.
replace pidstr_2009 = pidstr_2009_7 if pidstr_2009==.
clonevar pidstr_2013 = pidstr_2013_1
replace pidstr_2013 = pidstr_2013_3 if pidstr_2013==.
replace pidstr_2013 = pidstr_2013_5 if pidstr_2013==.
replace pidstr_2013 = pidstr_2013_7 if pidstr_2013==.
clonevar pidstr_2017 = pidstr_2017_1
replace pidstr_2017 = pidstr_2017_2 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_3 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_4 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_5 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_6 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_7 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_8 if pidstr_2017==.
replace pidstr_2017 = pidstr_2017_9 if pidstr_2017==.


clonevar print_pol_bild_2009 = print_pol_bild_2009_2
clonevar print_pol_bild_2013 = print_pol_bild_2013_2
clonevar print_pol_bild_2017 = print_pol_bild_2017_1

clonevar print_pol_faz_2009 = print_pol_faz_2009_2
clonevar print_pol_faz_2013 = print_pol_faz_2013_2
clonevar print_pol_faz_2017 = print_pol_faz_2017_1

clonevar print_pol_fr_2009 = print_pol_fr_2009_2
clonevar print_pol_fr_2013 = print_pol_fr_2013_2
clonevar print_pol_fr_2017 = print_pol_fr_2017_1

clonevar print_pol_other_2009 = print_pol_other_2009_2
clonevar print_pol_other_2013 = print_pol_other_2013_2
clonevar print_pol_other_2017 = print_pol_other_2017_1

clonevar print_pol_sz_2009 = print_pol_sz_2009_2
clonevar print_pol_sz_2013 = print_pol_sz_2013_2
clonevar print_pol_sz_2017 = print_pol_sz_2017_1

clonevar print_pol_taz_2009 = print_pol_taz_2009_2
clonevar print_pol_taz_2013 = print_pol_taz_2013_2
clonevar print_pol_taz_2017 = print_pol_taz_2017_1

clonevar print_pol_welt_2009 = print_pol_welt_2009_2
clonevar print_pol_welt_2013 = print_pol_welt_2013_2
clonevar print_pol_welt_2017 = print_pol_welt_2017_1

clonevar talk_pol_2009 = talk_pol_2009_1
clonevar talk_pol_2017 = talk_pol_2017_1

clonevar tv_pol_ard_2009 = tv_pol_ard_2009_2
clonevar tv_pol_ard_2013 = tv_pol_ard_2013_2
clonevar tv_pol_ard_2017 = tv_pol_ard_2017_1

clonevar tv_pol_other_2009 = tv_pol_pro7_2009_2
clonevar tv_pol_other_2013 = tv_pol_other_2013_2
clonevar tv_pol_other_2017 = tv_pol_other_2017_1

clonevar tv_pol_rtl_2009 = tv_pol_rtl_2009_2
clonevar tv_pol_rtl_2013 = tv_pol_rtl_2013_2
clonevar tv_pol_rtl_2017 = tv_pol_rtl_2017_1

clonevar tv_pol_sat1_2009 = tv_pol_sat1_2009_2
clonevar tv_pol_sat1_2013 = tv_pol_sat1_2013_2
clonevar tv_pol_sat1_2017 = tv_pol_sat1_2017_1

clonevar tv_pol_zdf_2009 = tv_pol_zdf_2009_2
clonevar tv_pol_zdf_2013 = tv_pol_zdf_2013_2
clonevar tv_pol_zdf_2017 = tv_pol_zdf_2017_1

clonevar vote_diff_2009 = vote_diff_2009_7
clonevar vote_diff_2013 = vote_diff_2013_3
clonevar vote_diff_2017 = vote_diff_2017_5

clonevar wissen_verb_2009 = wissen_verb_2009_6
clonevar wissen_verb_2013 = wissen_verb_2013_2
clonevar wissen_verb_2017 = wissen_verb_2017_1

clonevar wissen_vis_2013 = wissen_vis_2013_1
clonevar wissen_vis_2017 = wissen_vis_2017_2


*recode other variables
gen migrant = .
replace migrant = migrant_2009
replace migrant = migrant_2013 if migrant==.
replace migrant = migrant_2017 if migrant==.

drop migrant_*

*keep relevant variables
keep  id age_2009 age_2013 age_2017 sex east edu unemployed* nlf* hhsize* inc_* tp-wissen_vis_2017 ///
		vote_2009 vote_2013 vote_2017 migrant


* reshape data
reshape long polint_ effex1_ effex2_ effint1_ effint2_ unemployed_ nlf_ ewl_ hhsize_ imp_elec_ imp_elec_camp_ ///
	inc_ inet_pol_ itv_ partyid_ pidstr_ talk_pol_ vote_diff_ wissen_verb_ wissen_vis_ ///
	print_pol_bild_ print_pol_faz_ print_pol_fr_ print_pol_other_ print_pol_sz_ ///
	print_pol_taz_ print_pol_welt_ tv_pol_ard_ tv_pol_other_ tv_pol_rtl_ ///
	tv_pol_sat1_ tv_pol_zdf_ vote_ age_ , i(id) j(year) 

rename (*_) (*)

recode year (2009=0)(2013=1)(2017=2), gen(time)

* generate new vars 
egen effex = rowmean(effex1 effex2)
egen effint = rowmean(effint1 effint2)

egen print_pol = rowmean(print_pol_bild print_pol_faz print_pol_fr print_pol_other print_pol_sz print_pol_taz print_pol_welt)
egen tv_pol = rowmean(tv_pol_ard tv_pol_other tv_pol_rtl tv_pol_sat1 tv_pol_zdf)

egen media_pol = rowmean(print_pol tv_pol inet_pol)

clonevar female = sex

recode edu (2=1)(1 3=0)(else=.), gen(edu_med)
recode edu (3=1)(1 2=0)(else=.), gen(edu_high)

gen age18 = age-18
recode age (18/35=1 "18-35")(else=0 ">35"), gen(age1835)
recode age (18/25=1 "18-25")(else=0 ">25"), gen(age1825)

*assign mean value of income groups
recode inc (1=250)(2=750)(3=1250)(4=1750)(5=2250)(6=2750)(7=3500)(8=4500)(9=7500)(else=.), gen(hhinc)

*gen income variables
gen hhinc_pp = hhinc/hhsize
gen hhinc_pp_sqrt = hhinc/(sqrt(hhsize))

xtile hhinc_dec = hhinc, nq(10) 
xtile hhinc_dec_pp = hhinc_pp, nq(10) 
xtile hhinc_dec_pp_sqrt = hhinc_pp_sqrt, nq(10) 

*scale variables from 0-10
foreach var of varlist hhinc_dec hhinc_dec_pp hhinc_dec_pp_sqrt ewl polint effex effint imp_elec imp_elec_camp ///
	 inet_pol itv pidstr vote_diff wissen_verb wissen_vis print_pol tv_pol media_pol {
	sum `var'
	gen `var'10 = (`var' - r(min)) * (10/(r(max)-r(min)))
}


*save
save "wkp09-17_long.dta", replace


